﻿using BusinessObjects;
using Domain.Abstract;
using Domain.ModelMapper;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebUI.CommonHelper;
using WebUI.Models;

namespace WebUI.Controllers
{
    public class NccController : Controller
    {
        private readonly INccRepository _nccRepository;
        private readonly IXaPhuongRepository _xaPhuongRepository;
        private readonly INhomNccRepository _nhomNccRepository;

        //Constructor
        public NccController(INccRepository nccRepository, IXaPhuongRepository xaPhuongRepository, INhomNccRepository nhomNccRepository)
        {
            _nccRepository = nccRepository;
            _xaPhuongRepository = xaPhuongRepository;
            _nhomNccRepository = nhomNccRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListData(NccSearchModel nccSearchModel, string sort = "HoTen", string sortType = "asc", int pageIndex = 1)
        {
            var nccs = _nccRepository.Nccs;

            if (nccSearchModel != null)
            {
                if (!string.IsNullOrEmpty(nccSearchModel.HoTen))
                {
                    nccs = nccs.Where(x => x.HoTen.Contains(nccSearchModel.HoTen));
                }

                if (nccSearchModel.NamSinh.HasValue)
                {
                    nccs = nccs.Where(x => x.NamSinh == nccSearchModel.NamSinh);
                }

                if (nccSearchModel.IdQuanHuyen.HasValue)     //Select all QuanHuyen
                {
                    if (nccSearchModel.IdXaPhuong.HasValue)  //Selected XaPhuong
                    {
                        nccs = nccs.Where(x => x.IdNoiCapTruyLinh == nccSearchModel.IdXaPhuong);
                    }
                    else    //Select all XaPhuong
                    {
                        var lstXaPhuong = _xaPhuongRepository.GetXaPhuongsByQuanHuyenId((int)nccSearchModel.IdQuanHuyen).Select(x => x.Id);
                        nccs = nccs.Where(x => lstXaPhuong.Contains(x.Id));
                    }
                }

                if (nccSearchModel.LstIdNhomNcc != null && nccSearchModel.LstIdNhomNcc.Any())
                {
                    nccs = nccs.Where(n => n.NhomNcc.All(nh => nccSearchModel.LstIdNhomNcc.Contains(nh.Id)));
                }
            }

            var model = new NccListViewModel
                            {
                                Nccs = nccs.OrderBy(string.Format("{0} {1}", sort, sortType))
                                                            .Skip((pageIndex - 1) * CommonConst.ITEM_PER_PAGE)
                                                            .Take(CommonConst.ITEM_PER_PAGE)
                                                            .Select(x => new NccDto
                                                                             {
                                                                                 HoTen = x.HoTen,
                                                                                 NamSinh = x.NamSinh,
                                                                                 NoiCapTruyLinh = x.NoiCapTruyLinh,
                                                                                 NguyenQuan = x.NguyenQuan
                                                                             }),
                                PagingInfo = new PagingInfo
                                                 {
                                                     CurrentPage = pageIndex,
                                                     ItemsPerPage = CommonConst.ITEM_PER_PAGE,
                                                     TotalItems = nccs.Count()
                                                 },
                                CurrentSort = sort,
                                SortType = sortType,
                                NccSearchModel = nccSearchModel
                            };

            return PartialView(model);
        }

        public JsonResult ListDataJson(NccSearchModel nccSearchModel, string sort = "HoTen", string sortType = "asc", int pageIndex = 1)
        {
            var nccs = _nccRepository.Nccs;

            if (nccSearchModel != null)
            {
                if (!string.IsNullOrEmpty(nccSearchModel.HoTen))
                {
                    nccs = nccs.Where(x => x.HoTen.Contains(nccSearchModel.HoTen));
                }

                if (nccSearchModel.NamSinh.HasValue)
                {
                    nccs = nccs.Where(x => x.NamSinh == nccSearchModel.NamSinh);
                }

                if (nccSearchModel.IdQuanHuyen.HasValue)     //Select all QuanHuyen
                {
                    if (nccSearchModel.IdXaPhuong.HasValue)  //Selected XaPhuong
                    {
                        nccs = nccs.Where(x => x.IdNoiCapTruyLinh == nccSearchModel.IdXaPhuong);
                    }
                    else    //Select all XaPhuong
                    {
                        var lstXaPhuong = _xaPhuongRepository.GetXaPhuongsByQuanHuyenId((int)nccSearchModel.IdQuanHuyen).Select(x => x.Id).ToList();
                        nccs = nccs.Join(lstXaPhuong, n => n.IdNoiCapTruyLinh, x => x, (n, x) => n);
                    }
                }

                if (nccSearchModel.LstIdNhomNcc != null && nccSearchModel.LstIdNhomNcc.Any())
                {
                    nccs =
                        nccs.Where(
                            n => nccSearchModel.LstIdNhomNcc.All(nh => n.NhomNcc.
                                                                        Select(nhh => nhh.Id).
                                                                        Contains(nh)));
                }
            }

            var model = new NccListViewModel
            {
                Nccs = nccs.OrderBy(string.Format("{0} {1}", sort, sortType))
                                            .Skip((pageIndex - 1) * CommonConst.ITEM_PER_PAGE)
                                            .Take(CommonConst.ITEM_PER_PAGE)
                                            .Select(x => new NccDto
                                            {
                                                Id = x.Id,
                                                HoTen = x.HoTen,
                                                NamSinh = x.NamSinh,
                                                NoiCapTruyLinh = x.NoiCapTruyLinh,
                                                NguyenQuan = x.NguyenQuan
                                            }),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = pageIndex,
                    ItemsPerPage = CommonConst.ITEM_PER_PAGE,
                    TotalItems = nccs.Count()
                },
                CurrentSort = sort,
                SortType = sortType,
                NccSearchModel = nccSearchModel
            };

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DetailNcc(int nccId)
        {
            var ncc = _nccRepository.GetNccById(nccId);

            var nccDetailModel = new NccDetailModel
                                     {
                                         Ncc = ncc,
                                         NhomNccs = _nhomNccRepository.NhomNccs.Select(nh => new NhomNccDto
                                                                                                 {
                                                                                                     Id = nh.Id,
                                                                                                     Ten = nh.Ten
                                                                                                 })
                                     };

            //var n = Mapper.Map(_nccRepository.GetNccById(nccId));
            //n.NoiCapTLinh = Mapper.Map(ncc.NoiCapTLinh);
            //n.NoiCapTLinh.QuanHuyen = 

            return PartialView(nccDetailModel);
        }

        public ActionResult UpdateNcc(int nccId = 0)
        {
            var ncc = _nccRepository.GetNccById(nccId);

            return View(ncc);
        }
    }
}
